using Entitas;
using UnityEngine;

public class EliminateSystem : IExecuteSystem
{
    LogicContext mContext;

    public EliminateSystem(LogicContext context)
    {
        mContext = context;
    }

    public void Execute()
    {
        var entities = mContext.GetEntities(LogicMatcher.EliminateCell);

        foreach (var e in entities)
        {
            if (e.eliminateCell.CurFrame >= e.eliminateCell.TargetFrame)
            {
                if (e.grid.CellLst.Count > 0)
                {
                    e.grid.CellLst[0].ReplaceHp(e.grid.CellLst[0].hp.Value - 1);
                }
                e.RemoveEliminateCell();

                LogicApi.CreateCell(mContext, 1, Random.Range(1, 6), e.gridPos.Value, LogicApi.Time2Frame(0.4f));
            }
            else
            {
                e.eliminateCell.CurFrame++;
            }
        }
    }
}
